package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.product.service.DynamicService;
import com.wing.purchase.dao.SupplierDueDao;
import com.wing.purchase.model.entity.SupplierDue;
import com.wing.purchase.model.entity.SupplierDueEnclosure;
import com.wing.purchase.model.request.SupplierDueForm;
import com.wing.purchase.model.response.SupplierDueVO;
import com.wing.sell.model.entity.AgentDue;
import com.wing.sell.model.entity.AgentDueEnclosure;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 供应商尽调记录表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SupplierDueServiceImpl extends ServiceImpl<SupplierDueDao, SupplierDue> implements SupplierDueService {

    @Autowired
    private DynamicService dynamicService;


    @Override
    @Transactional
    public void add(SupplierDueForm supplierDueForm) throws  Exception{

        //校验

        SupplierDue supplierDue =new SupplierDue();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SupplierDue.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(supplierDueForm, supplierDue, copyOptions);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("pur_supplier_due");
        String day=simpleDateFormat.format(new Date());
        supplierDue.setCode("D"+day+code);
        supplierDue.setDay(Integer.valueOf(day));
        supplierDue.setNo(Integer.valueOf(code));
        supplierDue.setStatus(SupplierDue.Status.oncheck);
        baseMapper.insert(supplierDue);
    }

    @Override
    public List<SupplierDueVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SupplierDueVO> pageList4app(Page page, Map<String, Object> params) {
        List<SupplierDueVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SupplierDueVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void update(SupplierDueForm supplierDueForm) throws Exception {
        SupplierDue supplierDue = this.getById(supplierDueForm.getId());
        if (supplierDue==null){
            throw new BusinessException("修改失败,未找到尽调记录！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(SupplierDue.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(supplierDueForm, supplierDue, copyOptions);
        this.updateById(supplierDue);
    }

    @Override
    public boolean removeBySupId(Long supplierInfoId) {
        return baseMapper.removeBySupId(supplierInfoId);
    }
}
